<template>
  <div class="email-sender-container">
    <!-- 主要内容区域 -->
    <el-row :gutter="20">


      <!-- 右侧：邮箱通道列表 -->
      <el-col :xs="24" :sm="24" :md="18" :lg="18" class="sender-list-col">
        <el-card shadow="hover" class="sender-list-card">
          <div slot="header" class="card-header">
            <div class="header-title">
              <i class="el-icon-setting"></i>
              <span>邮箱域名</span>
            </div>
          </div>

          <!-- 搜索和筛选工具栏 -->
          <div class="search-toolbar">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
              <el-form-item label="邮箱名称" prop="sendName">
                <el-input
                  v-model="queryParams.sendName"
                  placeholder="请输入邮箱名称"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="邮箱域名" prop="sendEmail">
                <el-input
                  v-model="queryParams.sendEmail"
                  placeholder="请输入邮箱域名"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >申请</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
              >删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <!-- 邮箱通道表格 -->
          <div class="table-container">
            <el-table v-loading="loading" :data="senderList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="" align="center" prop="id" v-if="false" />

              <el-table-column label="" align="center" prop="emailHost" v-if="false" />
              <el-table-column label="邮箱名称" align="center" prop="sendName" />
              <el-table-column label="邮箱域名" align="center" prop="sendEmail" />
              <el-table-column label="审核状态" align="center" prop="auditStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditStatus === '0' || scope.row.auditStatus === 0" size="mini" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.auditStatus === '1' || scope.row.auditStatus === 1" size="mini" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.auditStatus === '2' || scope.row.auditStatus === 2" size="mini" type="danger">审核拒绝</el-tag>
          <el-tag v-else size="mini" type="warning">待审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核原因" align="center" prop="auditRemarks" width="200"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180"/>
      <el-table-column label="审核人" align="center" prop="auditorName" width="100"/>
      <el-table-column label="通道状态" align="center" prop="status"  width="90">
        <template slot-scope="scope">
        <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
      </template>
      </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>

          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
            class="pagination-container"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改ScrmEmailSender对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="邮箱名称" prop="sendName">
          <el-input v-model="form.sendName" placeholder="请输入邮箱名称" />
        </el-form-item>
        <el-form-item label="邮箱域名" prop="sendEmail">
          <el-input v-model="form.sendEmail" placeholder="请输入邮箱域名" />
        </el-form-item>
      </el-form>

      <!-- 域名配置提示 -->
      <div class="domain-tips">
        <div class="tips-header">
          <i class="el-icon-warning-outline"></i>
          <span>请填写您的自有域名，并支持域名DNS的配置，以便后期完成域名的验证！</span>
        </div>

        <div class="tips-content">
          <h4>注意事项：</h4>
          <div class="tip-item">
            <span class="tip-number">1、</span>
            <span class="tip-text">提示：域名可由英文字母、数字、-（中横线）构成，不能使用空格及特殊字符（如！、$、&、?、_等），不能含有中文。</span>
          </div>
          <div class="tip-item">
            <span class="tip-number">2、</span>
            <span class="tip-text">由于企业邮箱域名的特殊性，不建议您创建企业邮箱域名作为发信域名，可以创建与企业邮箱域名不同的子域，例如info.domain.com。</span>
          </div>
          <div class="tip-item">
            <span class="tip-number">3、</span>
            <span class="tip-text">批量/触发邮件的发信域名不建议共用，混发会影响触发邮件的发送；什么是批量/触发邮件？</span>
          </div>
          <div class="tip-item">
            <span class="tip-number">4、</span>
            <span class="tip-text">针对平台高危域名，添加后将无法通过配置验证</span>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmailManage, getEmailManage, delEmailManage, addEmailManage, updateEmailManage } from "@/api/email/EmailManage";
export default {
  name: "EmailSender",
  dicts: ['sys_normal_disable', 'audit_status', 'email_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ScrmEmailSender表格数据
      senderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前选中的类型
      selectedType: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        emailType: null,
        emailHost: null,
        sendName: null,
        sendEmail: null,
        password: null,
        createUserId: null,
        createUserName: null,
        status: null,
        apiUser: null,
        apiKey: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sendEmail: [
          { required: true, type:'email',message: "邮箱域名格式不正确", trigger: "blur" }
        ],
        sendName: [
          { required: true, message: "邮箱名称不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询ScrmEmailSender列表 */
    getList() {
      this.loading = true;
      this.queryParams.createUserId = this.$store.getters.userId;
      listEmailManage(this.queryParams).then(response => {
        this.senderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        emailType: null,
        emailHost: null,
        sendName: null,
        sendEmail: null,
        password: null,
        createUserId: null,
        status: null,
        apiUser: null,
        apiKey: null,
        auditStatus: null,
        auditReason: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.selectedType = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "申请邮箱域名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEmailManage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核邮箱通道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.emailType=1;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmailManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmailManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该数据项？').then(function() {
        return delEmailManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>

<style scoped>
.email-sender-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  margin: 0;
  background-color: #f5f7fa;
}

.email-sender-container > .el-row {
  flex-grow: 1;
  display: flex;
  margin: 0 !important;
}

.email-sender-container > .el-row > .el-col {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 0 !important;
}

.email-sender-container .el-card {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  margin: 0;
  border-radius: 0;
}

.email-sender-container .el-card >>> .el-card__body {
  flex-grow: 1;
  overflow: hidden;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-title i {
  font-size: 18px;
  color: #409eff;
}

.sender-type-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.type-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  color: #606266;
}

.type-item:hover {
  background-color: #ecf5ff;
  color: #409eff;
}

.type-item.active {
  background-color: #409eff;
  color: #ffffff;
}

.type-icon {
  margin-right: 8px;
  font-size: 16px;
}

.search-toolbar {
  margin-bottom: 16px;
  flex-shrink: 0;
}

.table-container {
  flex-grow: 1;
  overflow-y: auto;
  margin-bottom: 16px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  max-height: calc(100vh - 360px);
  min-height: 300px;
}

.el-table {
  margin-bottom: 0;
}

.pagination-container {
  text-align: right;
  margin-top: 0;
  padding: 0px 0;
  flex-shrink: 0;
  border-top: 1px solid #ebeef5;
  background-color: #fff;
}

.sender-type-col {
  width: 300px !important;
  min-width: 300px;
  max-width: 300px;
  flex: 0 0 300px;
  padding-right: 0 !important;
}

.sender-type-card {
  width: 100%;
  margin-right: 0;
  border-right: 1px solid #ebeef5;
  border-radius: 0;
}

.sender-list-col {
  flex: 1 !important;
  width: calc(100% - 300px) !important;
  max-width: none !important;
  padding-left: 0 !important;
}

.sender-list-card {
  width: 100%;
  border-radius: 0;
  border-left: none;
}

/* 域名提示样式 */
.domain-tips {
  margin-top: 20px;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.tips-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  font-size: 14px;
  color: #e6a23c;
  font-weight: 600;
}

.tips-header i {
  margin-right: 8px;
  font-size: 16px;
}

.tips-content h4 {
  margin: 0 0 12px 0;
  font-size: 14px;
  color: #303133;
  font-weight: 600;
}

.tip-item {
  display: flex;
  margin-bottom: 8px;
  line-height: 1.5;
}

.tip-number {
  color: #409eff;
  font-weight: 600;
  min-width: 20px;
  flex-shrink: 0;
}

.tip-text {
  color: #606266;
  font-size: 13px;
  line-height: 1.5;
}
</style>

